# coding=utf8
__author__ = 'lijian'
import logging
import datetime

import lamp
import rtu
import config
from protocol.uplink.packet import *
from utils import bcdUtil

# 终端日历时钟
def fn_2(dataUnit):
    logging.info("do query dateTime")
    now = datetime.datetime.now()
    data = bcdUtil.encodeDataTime(now)
    du = DataUnit(pn=0, fn=dataUnit.FN, data=data)
    return du


# 节能开关档位
def fn_3(dataUnit):
    point = dataUnit.PN
    value = rtu.manager.getLevel()
    du = DataUnit(pn=point, fn=dataUnit.FN, data=[value])
    return du


# 单灯轮询查询
def fn_4(dataUnit):
    logging.info("do query poll config")
    data = [int(config.poll["enable"]), int(config.poll["discover"])]
    du = DataUnit(pn=0, fn=dataUnit.FN, data=data)
    return du


# 模拟量查询
def fn_9(dataUnit):
    point = dataUnit.PN
    type = rtu.manager.getAnalogPointType(point)
    value = rtu.manager.endoceAnalogPointValue(point)

    data = []
    data.append(type)
    data.extend(value)

    du = DataUnit(pn=point, fn=dataUnit.FN, data=value)
    return du


# 状态量查询
def fn_10(dataUnit):
    point = dataUnit.PN
    value = rtu.manager.getSignalValue(point)
    du = DataUnit(pn=point, fn=dataUnit.FN, data=[value])
    return du


# 模拟量批量查询
def fn_11(dataUnit):
    count = dataUnit.rawData[0]
    start = dataUnit.rawData[1]

    data = []
    data.append(count)
    data.append(start)

    for point in range(start, start + count):
        type = rtu.manager.getAnalogPointType(point)
        value = rtu.manager.endoceAnalogPointValue(point)

        print("point:%d,type:%d,value:%r"%(point,type,value))

        data.append(point)
        data.append(type)
        data.extend(value)

    du = DataUnit(pn=0, fn=dataUnit.FN, data=data)
    return du


# 状态量批量查询
def fn_12(dataUnit):
    count = dataUnit.rawData[0]
    start = dataUnit.rawData[1]

    data = []
    data.append(count)
    data.append(start)

    for point in range(start, start + count):
        value = rtu.manager.getSignalValue(point)
        data.append(point)
        data.append(value)

    du = DataUnit(pn=0, fn=dataUnit.FN, data=data)
    return du


# 发现新单灯控制器查询
def fn_25(dataUnit):
    logging.info("do query new term")

    # data = [5]
    # for i in range(1, 1 + 5):
    #     data.extend([i, 0x88, 0, 0, 0, 0])

    du = DataUnit(pn=0, fn=dataUnit.FN, data=[])
    return du


# 单灯运行状态查询
def fn_33(dataUnit):
    holderNo = dataUnit.PN
    holder = lamp.manager.findHolder(holderNo)
    if not holder:
        return False
    else:
        data = [holder.onOffState(), holder.runState()]
        du = DataUnit(pn=dataUnit.PN, fn=dataUnit.FN, data=data)
        return du


# 单灯电压数据查询
def fn_34(dataUnit):
    holderNo = dataUnit.PN
    holder = lamp.manager.findHolder(holderNo)
    if not holder:
        return False
    else:
        data = lamp.Holder.encodeV(holder)
        du = DataUnit(pn=dataUnit.PN, fn=dataUnit.FN, data=data)
        return du


# 单灯电流数据查询
def fn_35(dataUnit):
    holderNo = dataUnit.PN
    holder = lamp.manager.findHolder(holderNo)
    if not holder:
        return False
    else:
        data = lamp.Holder.encodeA(holder)
        du = DataUnit(pn=dataUnit.PN, fn=dataUnit.FN, data=data)
        return du


# 单灯有功功率数据查询
def fn_36(dataUnit):
    holderNo = dataUnit.PN
    holder = lamp.manager.findHolder(holderNo)
    if not holder:
        return False
    else:
        data = lamp.Holder.encodeAP(holder)
        du = DataUnit(pn=dataUnit.PN, fn=dataUnit.FN, data=data)
        return du


# 单灯功率因数查询
def fn_37(dataUnit):
    holderNo = dataUnit.PN
    holder = lamp.manager.findHolder(holderNo)
    if not holder:
        return False
    else:
        data = lamp.Holder.encodePF(holder)
        du = DataUnit(pn=dataUnit.PN, fn=dataUnit.FN, data=data)
        return du


# 单灯视在功率查询
def fn_38(dataUnit):
    holderNo = dataUnit.PN
    holder = lamp.manager.findHolder(holderNo)
    if not holder:
        return False
    else:
        data = lamp.Holder.encodeAPP(holder)
        du = DataUnit(pn=dataUnit.PN, fn=dataUnit.FN, data=data)
        return du


# 单灯无功功率查询
def fn_39(dataUnit):
    holderNo = dataUnit.PN
    holder = lamp.manager.findHolder(holderNo)
    if not holder:
        return False
    else:
        data = lamp.Holder.encodeRP(holder)
        du = DataUnit(pn=dataUnit.PN, fn=dataUnit.FN, data=data)
        return du